package com.flowershop.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/";
    private static final String DB_NAME = "flower_shop";
    private static final String FULL_URL = URL + DB_NAME;
    private static final String USER = "root";
    private static final String PASSWORD = "123456";
    
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            createDatabaseIfNotExists();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    private static void createDatabaseIfNotExists() {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
             Statement stmt = conn.createStatement()) {
            
            stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS " + DB_NAME);
            System.out.println("Database checked/created successfully!");
            
        } catch (Exception e) {
            System.err.println("Failed to create database: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection() throws Exception {
        return DriverManager.getConnection(FULL_URL, USER, PASSWORD);
    }
} 